package com.ruoyi.system.service.module.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.module.Parameter;
import com.ruoyi.system.mapper.module.ParameterMapper;
import com.ruoyi.system.service.module.IParameterService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 配置参数表Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-04
 */
@Service
public class ParameterServiceImpl extends ServiceImpl<ParameterMapper, Parameter> implements IParameterService {

	@Resource
	ParameterMapper parameterMapper;

	@Resource
	protected RedisTemplate<String, Object> redisTemplate;

	@Override
	@Cacheable(value = "parameter", key = "'parameter_' + #id", unless = "#result == null")
	public Parameter getParameterById(int id) {
		return parameterMapper.selectById(id);
	}

	@Override
	public void resetParameter(int id) {
		String key = "parameter_" + id;
		Parameter parameter = parameterMapper.selectOneByColumn("is_default",1);
		redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(parameter));
	}

}
